let { api, imgUrl } = require("./../../utils/api.js");
let labels = [
  { labelId: 1, text: "卫生不好" },
  { labelId: 2, text: "分量少" },
  { labelId: 3, text: "太辣" },
  { labelId: 4, text: "太咸" },
  { labelId: 5, text: "太甜" },
  { labelId: 6, text: "太酸" },
  { labelId: 7, text: "太苦" },
  { labelId: 8, text: "无味" },
  { labelId: 9, text: "干净卫生" },
  { labelId: 10, text: "卖相不错" },
  { labelId: 11, text: "味道一般" },
  { labelId: 12, text: "分量足" },
  { labelId: 13, text: "味道赞" },
  { labelId: 14, text: "有营养" }
];
Page({
  data: {
    checkTab: "canteen",
    score1: "fa fa-star star", //评价星级的class值
    score2: "fa fa-star-half-o half-star",
    score3: "fa fa-star def-star",
    imgUrl
  },

  onLoad: function() {
    // this.getRestaurantEvl();
  },

  onShow: function() {
    this.getRestaurantEvl();
  },

  onPullDownRefresh: function() {
    //下拉刷新数据
    this.getRestaurantEvl();
    setTimeout(() => {
      wx.stopPullDownRefresh();
      wx.showToast({
        title: "成功更新评论信息 ！",
        icon: "none",
        duration: 2000
      });
    }, 1500);
  },

  // 将标签代号转换成实际的汉字
  toTaglist: function(arr) {
    let newArr = arr.map(val => {
      for (let i = 0; i < labels.length; i++) {
        if (labels[i].labelId === val) {
          return labels[i].text;
        }
      }
    });
    return newArr;
  },
  //获取我的评价(包括菜系和餐厅)
  getRestaurantEvl: function() {
    let that = this;
    let obj = {
      userId: wx.getStorageSync("userInfo").userId
    };
    console.log(obj);
    wx.request({
      url: api + "getMyEvaluation.do",
      method: "POST",
      data: obj,
      success: function(res) {
        // console.log(res.data.data);
        console.log("我的评价：我刷新了！");
        if (res.data.status === 1) {
          let evaluationRestaurantList = res.data.data.evaluationRestaurantList.map(
            val => {
              val.evaluationDate = val.evaluationDate.slice(5, 17);
              val.evaluationContent = decodeURIComponent(val.evaluationContent);
              return val;
            }
          );
          let evaluationDishList = res.data.data.evaluationDishList.map(val => {
            val.evaluationDate = val.evaluationDate.slice(5, 17);
            val.newTaglist = that.toTaglist(val.taglist);
            val.evaluationContent = decodeURIComponent(val.evaluationContent);
            return val;
          });
          that.setData({
            canteenData: evaluationDishList,
            restaurantData: evaluationRestaurantList
          });
        } else {
          wx.showToast({
            title: "评价获取失败，请稍后重试!",
            icon: "none",
            duration: 2000
          });
        }
      },
      fail: function(res) {
        wx.showToast({
          title: "网络错误，请稍后重试",
          icon: "none",
          duration: 2000
        });
      }
    });
  },
  // 点击图片进行放大
  imgPreview: function(e) {
    let src = e.currentTarget.dataset.src;
    let srcArr = [src];
    let current = src;
    if (Object.prototype.toString.call(src) == "[object Array]") {
      srcArr = src.map(val => {
        return imgUrl + val;
      });
      current = e.currentTarget.dataset.current;
    }
    wx.previewImage({
      current,
      urls: srcArr // 需要预览的图片http链接列表
    });
  },
  //删除评论
  delEvaluate: function(id, types) {
    let that = this;
    let urls = "";
    let arrData = [];
    let name = "";
    if (Number(types) === 1) {
      urls = "deleteRestaurantEvaluation.do";
      arrData = this.data.restaurantData;
      name = "restaurantData";
    } else if (Number(types) === 2) {
      urls = "deleteDishEvaluation.do";
      arrData = this.data.canteenData;
      name = "canteenData";
    }
    wx.request({
      url: api + urls,
      method: "POST",
      data: {
        evaluationId: [id]
      },
      success: function(res) {
        if (res.data.status === 1) {
          for (let i = 0; i < arrData.length; i++) {
            if (arrData[i].evaluationId === Number(id)) {
              arrData.splice(i, 1);
            }
          }
          let obj = {
            [name]: arrData
          };
          that.setData(obj, () => {
            wx.showToast({
              title: "删除成功 !",
              icon: "none",
              duration: 2000
            });
          });
        } else {
          wx.showToast({
            title: "删除失败，请稍后重试!",
            icon: "none",
            duration: 2000
          });
        }
      },
      fail: function(res) {
        wx.showToast({
          title: "网络错误，请稍后重试",
          icon: "none",
          duration: 2000
        });
      }
    });
  },

  // 跳转菜系详情
  gotoDishDetails: function(e) {
    let dishId = e.currentTarget.dataset.dishid; //data-dishId会大写转小写
    wx.navigateTo({
      url: "./../details/details?dishId=" + dishId
    });
  },

  //删除评论的弹框
  delModal: function(e) {
    let that = this;
    wx.vibrateShort();
    wx.showModal({
      title: "温馨提示",
      content: "确定删除本条评论？",
      success: function(res) {
        if (res.confirm) {
          console.log("用户点击确定,删除ID为" + e.currentTarget.id + "的数据");
          // 请求接口进行评论删除，成功后同时删除本地的这条数据（假象），尽量不刷新
          that.delEvaluate(e.currentTarget.id, e.currentTarget.dataset.types);
        } else if (res.cancel) {
          console.log("用户点击取消");
        }
      }
    });
  },

  // 切换查看评论
  checkEvaluation: function(e) {
    let checkTab = e.target.id === "restaurant" ? "restaurant" : "canteen";
    this.setData({
      checkTab
    });
  }
});
